package cn.meye11.crm.service;

import cn.meye11.crm.common.RestResponse;
import cn.meye11.crm.common.StringUtils;
import cn.meye11.crm.domain.entity.ItemEntity;
import cn.meye11.crm.domain.query.ItemQuery;
import cn.meye11.crm.mapper.ItemMapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import cn.meye11.crm.domain.model.Item;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 项目表(Item)表服务实现类
 * 默认CRUD操作
 * @author makejava
 * @since 2023-11-18 19:20:43
 */
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class ItemService {

    private final ItemMapper itemMapper;
    private final ObjectMapper objectMapper;

    // 代码写到此区域
    
    
    /************************************* 请在上方编写新代码 **************************************/
    // 此区域为自动生成的通用代码 尽量不直接修改
    
    @Transactional(rollbackFor = Exception.class)
    public Long save(Item model) {
        if (StringUtils.isBlank(model.getCreatedBy())) {
            throw new RuntimeException("创建人不能为空");
        }
        if (model.getId() != null) {
                throw new RuntimeException("主键id不可自定义");
        }
        if (model.getVersion() != null) {
            throw new RuntimeException("数据版本号不可自定义");
        }
        if (model.getTime() != null) {
            throw new RuntimeException("数据创建时间不可自定义");
        }
        if (model.getLast() != null) {
            throw new RuntimeException("数据修改时间不可自定义");
        }
        
        ItemEntity entity = new ItemEntity(model);
    
        itemMapper.insertSelective(entity);

        return entity.getId();
    }
    
    public void remove(Long id) {
        if (id == null) {
            throw new RuntimeException("ID不能为空");
        }

        ItemEntity entity = itemMapper.selectByPrimaryKey(id);
        if (entity == null) {
            throw new RuntimeException("ID:" + id + "不存在");
        }
        itemMapper.deleteByPrimaryKey(id);
    }
    
    @Transactional(rollbackFor = Exception.class)
    public void update(Item model) {
    
        if(model.getId() == null){
            throw new RuntimeException("ID不能为空");
        }
        
        if (StringUtils.isBlank(model.getModifiedBy())) {
            throw new RuntimeException("修改人不能为空");
        }
        
        ItemEntity entity = itemMapper.selectByPrimaryKey(model.getId());
        if (entity == null) {
            throw new RuntimeException("ID:" + model.getId() + "不存在");
        }
        
        ItemEntity save = new ItemEntity(model);
        save.setLast(LocalDateTime.now());
        save.setTime(entity.getTime());
        itemMapper.updateByPrimaryKey(save);
    }
    
    public Item get(Long id) {
    ItemEntity entity = itemMapper.selectByPrimaryKey(id);
    return entity == null ? null : new Item(entity);
    }
    
    public Long count(ItemQuery query) {
        return itemMapper.count(query);
    }
    
    public RestResponse<List<Item>> list(ItemQuery query) {
        // 单次查不得大于32767条数据，否则需要分页
        query.setLimit((int) Short.MAX_VALUE);
        return page(query);
    }
    
    public RestResponse<List<Item>> page(ItemQuery query) {
        PageHelper.startPage(query.getPage(), query.getLimit());
        List<ItemEntity> list = itemMapper.list(query);
        PageInfo<ItemEntity> count = new PageInfo<>(list);
        List<Item> collect = list.stream().map(Item::new).collect(Collectors.toList());
        return RestResponse.success(count.getTotal(), collect);
    }

}
